package com.bw.lib_common.widget.nine_pic_grid

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.view.RoundedCorner
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.bw.lib_common.R
import com.bw.lib_common.dp
import com.bw.lib_common.utils.ToastUtils
import com.bw.lib_image.GlideApp
import javax.annotation.meta.When

/**
 * @author ytx
 * @date 2025-09-26 08:14
 * @description 多布局
 */
class NinePicsAdapter(
    private val mPicList:List<Image>,
    private val imageClick:((Int)->Unit)? = null
):RecyclerView.Adapter<NinePicsAdapter.NinePicsViewHolder>() {


    inner class NinePicsViewHolder(val itemView:ImageView,val width:Int,val height:Int):RecyclerView.ViewHolder(itemView){
        init {
            itemView.layoutParams = RecyclerView.LayoutParams(width,height) //设置宽和高
        }
    }

    override fun onCreateViewHolder(p0: ViewGroup, viewType: Int): NinePicsViewHolder {
        val imageView = ImageView(p0.context).apply {
            scaleType = ImageView.ScaleType.CENTER_CROP
            background = context.getDrawable(R.drawable.shape_bg_mask_round_4dp) //背景
            foreground = context.getDrawable(R.drawable.shape_bg_normal_ripple_with_round_mask) //前景
        }
        when(viewType){
            NINE.value ->{
                return NinePicsViewHolder(imageView,110.dp,110.dp)
            }
            PORTRAIT.value ->{
                return NinePicsViewHolder(imageView,230.dp,310.dp)
            }
            LANDSCAPE.value ->{
                return NinePicsViewHolder(imageView,230.dp,170.dp)
            }
            else ->{
                return NinePicsViewHolder(imageView,110.dp,110.dp)
            }
        }

    }

    override fun getItemCount(): Int {
        return mPicList.size
    }

    override fun onBindViewHolder(holder: NinePicsViewHolder, p0: Int) {
        val position = p0
        val isVideo = mPicList[position].contentType?.contains("video")
        val imageUrl = mPicList[position].run {
            if (isVideo == true){
                url
            }else{
                if (thumbnailUrl.isNullOrEmpty()){
                    url
                }else{
                    thumbnailUrl
                }
            }
        }
        val imageView =  holder.itemView as ImageView
        //Glide加载图片
        val glideRequest = GlideApp.with(imageView)
            .load(imageUrl)
            .listener(object :RequestListener<Drawable>{
                override fun onLoadFailed(
                    p0: GlideException?,
                    p1: Any?,
                    p2: Target<Drawable>?,
                    p3: Boolean
                ): Boolean { //glide图片加载失败
                    imageView.setOnClickListener {
                        ToastUtils.showShort(imageView.context,"图片加载失败")
                    }
                    return  false
                }

                override fun onResourceReady(
                    p0: Drawable?,
                    p1: Any?,
                    p2: Target<Drawable>?,
                    p3: DataSource?,
                    p4: Boolean
                ): Boolean {//图片准备完成，可以点击了
                    imageView.setOnClickListener {
                        imageClick?.invoke(position)
                    }
                    return false
                }
            })
        if (getItemViewType(position) == NINE.value){
            glideRequest.override(110.dp,110.dp)
        }else if (getItemViewType(position) == PORTRAIT.value){
            glideRequest.override(230.dp,310.dp)
        }else if (getItemViewType(position) == LANDSCAPE.value){
            glideRequest.override(230.dp,170.dp)
        }else{
            glideRequest.override(110.dp,110.dp)
        }
        glideRequest.transform(RoundedCorners(4.dp)).into(imageView)

    }
    /**
     * 多布局
     */
    override fun getItemViewType(position: Int): Int {
        return if (mPicList.isEmpty()){
             NONE.value
        }else if (mPicList.size == 1){
            val image = mPicList[0]
            if (image.imageWidth > image.imageHeight){
                 LANDSCAPE.value
            }else{
                PORTRAIT.value
            }
        }else{
            NINE.value
        }
    }
}



sealed class ViewType(val value: Int)
object NONE : ViewType(1)
object NINE : ViewType(2)
object PORTRAIT : ViewType(3) //竖屏
object LANDSCAPE : ViewType(4) //横屏
//object VIDEO : ViewType(5)